package com.financing.wap.weixin.util;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Formatter;
import java.util.UUID;

import com.financing.wap.util.StringUtil;

/**
 * 
 * 微信签名 工具类
 * @author lisu
 *
 */
public class WeixinSign {
    /**
     * 微信 发送红包 生成签名
     * @param Object
     * @return String
     */
    public static String SendRedPackSign(Object object,String key) {
    	
    	//获取对象属性名
    	Field[] fields=object.getClass().getDeclaredFields();  
        String[] fieldNames=new String[fields.length];  
	    for(int i=0;i<fields.length;i++){  
	        fieldNames[i]=fields[i].getName();  
	    }  
	    
        // 将参数进行字典序排序  
        Arrays.sort(fieldNames);  
        
        //如果参数的值为空不参与签名
        StringBuilder stringA = new StringBuilder(); 
        StringBuilder stringSignTemp = new StringBuilder();
        String signature = "";
        for(int i=0;i<fieldNames.length;i++){ 
        	Object fieldValue = getFieldValueByName(fieldNames[i],object);
        	if(fieldValue instanceof java.lang.String ){
        		if(!StringUtil.isNullOrEmptyStr(fieldValue.toString())){
            		if(i != 0){
            			stringA.append("&");
            		}
            		stringA.append(fieldNames[i]);
            		stringA.append("=");
            		stringA.append(fieldValue);
        		}
        	}
        	else {
        		if(fieldValue !=null){
	        		if(i != 0){
	        			stringA.append("&");
	        		}
	        		stringA.append(fieldNames[i]);
	        		stringA.append("=");
	        		stringA.append(fieldValue);
        		}
        	}
        }
        stringSignTemp.append(stringA.toString());
        stringSignTemp.append("&key=");
        stringSignTemp.append(key);
        try
        {
            MessageDigest crypt = MessageDigest.getInstance("MD5");
            crypt.reset();
            crypt.update(stringSignTemp.toString().getBytes("UTF-8"));
            signature = byteToHex(crypt.digest()).toUpperCase();
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

        return signature;
    }
    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash)
        {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
    public static String create_nonce_str() {
        return UUID.randomUUID().toString().trim().replaceAll("-", "");  
    }

    @SuppressWarnings("unused")
	private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }
    private static Object getFieldValueByName(String fieldName, Object o) {  
        try {    
            String firstLetter = fieldName.substring(0, 1).toUpperCase();    
            String getter = "get" + firstLetter + fieldName.substring(1);    
            Method method = o.getClass().getMethod(getter, new Class[] {});    
            Object value = method.invoke(o, new Object[] {});    
            return value;    
        } catch (Exception e) {    
            return null;    
        }    
    } 
}
